package com.china_goldcard.myselectview;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import java.util.List;

/**
 * Created by jk on 16/10/27.
 */

public class MySelectedView extends RelativeLayout {

    private RecyclerView mySelectView;

    private float itemH;

    private boolean isShow;

    private float orginalH;

    private Activity activity;

    public MySelectedView(Context context) {
        this(context,null);
    }

    public MySelectedView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public MySelectedView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getConfig(context,attrs);
        initViews(context);
    }

    /**
     * 从xml中获取配置信息
     */
    private void getConfig(Context context, AttributeSet attrs) {
        //TypedArray是一个数组容器用于存放属性值
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MySelectedView);
        itemH = attributes.getDimension(R.styleable.MySelectedView_select_view_itemH,dp2px(20));
        //用完务必回收容器
        attributes.recycle();
    }

    /**
     * 初始化视图
     * @param context
     */
    private void initViews(Context context){
        LayoutInflater.from(context).inflate(R.layout.select_view,this,true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mySelectView = (RecyclerView)findViewById(R.id.my_select_view);
        mySelectView.setLayoutManager(layoutManager);
        setVisibility(GONE);
    }

    /**
     * 切换显示或隐藏
     */
    public void toggle(){
        if (isShow){
            hide();
        }else {
            show();
        }
    }

    /**
     * 隐藏
     */
    private void hide(){
        AnimatorUtil.animHeightToView(activity,this,false,200);
        isShow = false;
    }

    /**
     * 显示
     */
    private void show(){
        isShow = true;
        this.getParent().bringChildToFront(this);
        AnimatorUtil.animHeightToView(activity,this,true,200);
    }

    private MySelectViewAdapter.OnItemClickListener onItemClickListener;

    /**
     * 加载数据
     */
    public <M extends MySelectItemModel,V extends MySelectViewHolder> MySelectedView loadData(Activity activity,final List<M> dataList,MySelectDataAdapter<V,M> dataAdapter){
        this.activity = activity;
        MySelectViewAdapter<M,V> adapter ;
        if (dataAdapter != null){
            adapter = new MySelectViewAdapter<M,V>(dataList,dataAdapter);
        }else{
            adapter = new MySelectViewAdapter<M,V>(dataList,new MyDefaultDataAdapter(getContext()));
        }
        adapter.setItemH(itemH);
        adapter.setOnItemClickListener(new MySelectViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, MySelectItemModel model) {
               if(onItemClickListener !=null){
                   onItemClickListener.onItemClick(v,model);
               }
                hide();
            }

            @Override
            public void onLongItemClick(View v, MySelectItemModel model) {
                if(onItemClickListener !=null){
                    onItemClickListener.onItemClick(v,model);
                }
                hide();
            }
        });
        mySelectView.setAdapter(adapter);
        orginalH = 5 * itemH;
        return this;
    }

    public void setOnItemClickListener(MySelectViewAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 加载数据
     * @param dataList
     * @param <M>
     * @param <V>
     */
    public <M extends MySelectItemModel,V extends MySelectViewHolder> MySelectedView loadData(Activity activity,List<M> dataList){
       return this.loadData(activity,dataList,null);
    }

    public float dp2px(float dp) {
        final float scale = getResources().getDisplayMetrics().density;
        return dp * scale + 0.5f;
    }

    public float sp2px(float sp) {
        final float scale = getResources().getDisplayMetrics().scaledDensity;
        return sp * scale;
    }

}
